package com.pignest.hotel.ui.reception;

import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.event.PagingEvent;

import com.pignest.hotel.beans.Member;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.MoneyUtil;

public class LivingMemberListbox extends Listbox {

	/**
	 * 入住会员查询
	 */
	private static final long serialVersionUID = 4251365483506858468L;
	private Paging page;
	private MemberService memberService;
	private String name;
	private String mobile;
	private Textbox mNameTexbox;
	private Textbox mCardNo;
	private Textbox mMobile;
	private Textbox mEmail;
	private Textbox mMemLevel;
	private Textbox mBalance;
	private Checkbox isMemberChb; //是否是会员
	
   public LivingMemberListbox(){
	   memberService=Services.getMemberService();
   }
   public void onCreate() {
	   mNameTexbox =(Textbox)getFellow("mNameTexbox");
	   mCardNo     =(Textbox)getFellow("mCardNo");
	   mMobile     =(Textbox)getFellow("mMobile");
	   mEmail      =(Textbox)getFellow("mEmail");
	   mMemLevel   =(Textbox)getFellow("mMemLevel");
	   mBalance    =(Textbox)getFellow("mBalance");
	   isMemberChb =(Checkbox)getFellow("isMemberChb");
	   page =  (Paging)getFellow("pagMember");
	   final int PAGE_SIZE = page.getPageSize();
	   page.addEventListener("onPaging", new EventListener() {
		      public void onEvent(Event event) {
		        	PagingEvent pe = (PagingEvent) event;
		        	int pgno = pe.getActivePage();
		        	int start = pgno*PAGE_SIZE;
		        	queryMem(start,PAGE_SIZE);
		      	}
		    	});
   }
   public void queryMem(int start,int pageSize){
	   this.getItems().clear();
	   StringBuffer hql=new StringBuffer();
	   hql.append(" from Member b where 1=1");
	   if(!name.equals(""))
		   hql.append(" and b.name like '%").append(name).append("%'");
	   if(!mobile.equals(""))
		   hql.append(" and b.mobile like '%").append(mobile).append("%'");
	   List l =(List)memberService.find(hql.toString());
	   int Total =l.size();
	   List<Member> members=memberService.QueryBy(hql.toString(), start, pageSize);
	   page.setTotalSize(Total);
	   for(int i=0;i<members.size();i++){
			final Member member = members.get(i);
			Listitem listitem = new Listitem();
			listitem.setValue(member.getId());
			listitem.setParent(this);
			listitem.appendChild(new Listcell(member.getName()));
			listitem.appendChild(new Listcell(member.getMobile()));
			if(member.getMemberLevel()!=null)
				listitem.appendChild(new Listcell(member.getMemberLevel().getMemberlevel()));
			else
				listitem.appendChild(new Listcell(""));
			listitem.appendChild(new Listcell(MoneyUtil.toString(member.getBalance())));
			Integer gral=member.getIntegral();
			if(gral!=null)
			  listitem.appendChild(new Listcell(gral.toString()));
			else
				listitem.appendChild(new Listcell("0"));
			listitem.appendChild(new Listcell(member.getMail()));
			listitem.addEventListener(Events.ON_CLICK,  new EventListener(){
				public void onEvent(Event event) throws Exception { 
					//this.selectRoom=r;
					mNameTexbox.setValue(member.getName());
					mCardNo.setValue(member.getCardNo());
					mMobile.setValue(member.getMobile());
					mEmail.setValue(member.getMail());
					mBalance.setValue(MoneyUtil.toString(member.getBalance()));
					if(member.getMemberLevel()!=null)
					   mMemLevel.setValue(member.getMemberLevel().getMemberlevel());
					else
						mMemLevel.setValue("");
					isMemberChb.setChecked(true);
						
				}
			});
		}
   }
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getMobile() {
	return mobile;
}
public void setMobile(String mobile) {
	this.mobile = mobile;
}
   
}
